package com.yangzb.flink.sql.sample;

import org.apache.commons.cli.*;

/**
 * @author: yangzb
 * @date 2023/2/21 10:57 AM
 **/
public class CliOptionsParser {

    public static final Option OPTION_JOB_NAME = Option
            .builder("n")
            .required(true)
            .longOpt("job_name")
            .numberOfArgs(1)
            .argName("任务名")
            .desc("任务名.")
            .build();

    public static final Option OPTION_SQL_FILE = Option
            .builder("f")
            .required(true)
            .longOpt("file")
            .numberOfArgs(1)
            .argName("SQL file path")
            .desc("The SQL file path.")
            .build();

    public static final Options CLIENT_OPTIONS = getClientOptions(new Options());

    public static Options getClientOptions(Options options) {
        options.addOption(OPTION_SQL_FILE);
        options.addOption(OPTION_JOB_NAME);
        return options;
    }

    // --------------------------------------------------------------------------------------------
    //  Line Parsing
    // --------------------------------------------------------------------------------------------

    public static CliOptions parseClient(String[] args) {
        if (args.length < 1) {
            throw new RuntimeException(" -n <job_name> -f <sql-file>");
        }
        try {
            DefaultParser parser = new DefaultParser();
            CommandLine line = parser.parse(CLIENT_OPTIONS, args, true);
            return new CliOptions(
                    line.getOptionValue(CliOptionsParser.OPTION_SQL_FILE.getOpt()),
                    line.getOptionValue(CliOptionsParser.OPTION_JOB_NAME.getOpt())
            );
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
